package com.tanhua.server.service;

import com.tanhua.commons.templates.HuanXinTemplate;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.domain.mongo.UserUnLike;
import com.tanhua.domain.vo.TanhuaVo;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.mongo.FriendApi;
import com.tanhua.dubbo.api.mongo.RecommendUserApi;
import com.tanhua.dubbo.api.mongo.UserLikeApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class TanhuaService {

    @Reference
    private RecommendUserApi recommendUserApi;
    @Reference
    private UserLikeApi userLikeApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Reference
    private FriendApi friendApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    public ResponseEntity<Object> tanhuaRecommendation() {
        Long userId = UserHolder.getUserId();
        List<RecommendUser> list = recommendUserApi.tanhuaRecommendation(userId);

        List<Long> likeId = new ArrayList<>();
        List<Long> unLikeId = new ArrayList<>();
        List<Long> friendId = new ArrayList<>();


        List<UserLike> like = userLikeApi.findLikeId(userId);
        for (UserLike userLike : like) {
            likeId.add(userLike.getLikeUserId());
        }

        List<UserUnLike> unLike = userLikeApi.findUnLikeId(userId);
        for (UserUnLike userUnLike : unLike) {
            unLikeId.add(userUnLike.getUnLikeUserId());
        }

        List<Friend> friends = friendApi.findFriend(userId);
        for (Friend friend : friends) {
            friendId.add(friend.getFriendId());
        }

        //4. 创建返回的vo集合
        List<TanhuaVo> voList = new ArrayList<>();
        //5. 遍历查询结果
        if (!CollectionUtils.isEmpty(list)) {
            for (RecommendUser recommendUser : list) {
                Long recommendUserId = recommendUser.getRecommendUserId();
                if(likeId.contains(recommendUserId) || unLikeId.contains(recommendUserId) || friendId.contains(recommendUserId)){
                    continue;
                }

                TanhuaVo vo = new TanhuaVo();
                // 根据用户id查询
                UserInfo userInfo = userInfoApi.findById(recommendUserId);
                if (userInfo != null) {
                    BeanUtils.copyProperties(userInfo, vo);
                    vo.setId(userInfo.getId());
                    if (userInfo.getTags() != null) {
                        vo.setTags(userInfo.getTags().split(","));
                    }
                }
                voList.add(vo);
            }

        }
        return ResponseEntity.ok(voList);
    }

    public ResponseEntity<Object> fanLove(Long likeUserId){
        if(userLikeApi.find(UserHolder.getUserId(),likeUserId)!=null){
            userLikeApi.delete(UserHolder.getUserId(),likeUserId);

            friendApi.save(UserHolder.getUserId(),likeUserId);

            //huanXinTemplate.contactUsers(UserHolder.getUserId(),likeUserId);  //环信功能

            return ResponseEntity.ok(null);
        }
        userLikeApi.save(UserHolder.getUserId(),likeUserId);
        return ResponseEntity.ok(null);
    }


    public ResponseEntity<Object> fanUnLove(Long unLikeUserId){
        userLikeApi.saveUnLike(UserHolder.getUserId(),unLikeUserId);
        return ResponseEntity.ok(null);
    }
}
